package com.liuceng.zen.service;

import com.liuceng.zen.mapper.GdtCreativeMapper;
import com.liuceng.zen.mapper.NewRptFrontCampaignDailyMapper;
import com.liuceng.zen.mapper.RptFrontAdGroupDailyMapper;
import com.liuceng.zen.mapper.RptFrontCampaignDailyMapper;
import com.liuceng.zen.web.auth.utils.TimeProcessingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DashboardService {
  @Autowired
  GdtCreativeService creativeService;
  @Autowired
  GdtCreativeMapper gdtCreativeMapper;
  @Autowired
  RptFrontCampaignDailyMapper rptFrontCampaignDailyMapper;
  @Autowired
  RptFrontAdGroupDailyMapper rptFrontAdGroupDailyMapper;
  @Autowired
  NewRptFrontCampaignDailyMapper newRptFrontCampaignDailyMapper;

  public Integer findMemberTotalAdNum(Integer currentUserId) {
    Map<String, Object> params = new HashMap<>();
    params.put("memberId", currentUserId);
    return gdtCreativeMapper.findMemberTotalAdNum(params);
  }

  // public Map<String, Object> findTotalData(Map<String, Object> params) {
  // Map<String, Object> result = rptFrontCampaignDailyMapper.findRptData(params);
  // if (result != null) {
  // result.put("ctr",
  // result == null || result.get("pv") == null
  // || 0 == Integer.parseInt(result.get("pv").toString()) ? new BigDecimal(0)
  // : new BigDecimal(Integer.parseInt(result.get("pc").toString()) * 100)
  // .divide(new BigDecimal(result.get("pv").toString()), 3, RoundingMode.UP));
  //
  // }
  // return result;
  // }

  public Map<String, Object> findTotalData(Map<String, Object> params) {
    Map<String, Object> result = newRptFrontCampaignDailyMapper.findRptData(params);
    if (result != null) {
      result.put("ctr",
          result == null || result.get("pv") == null
              || 0 == Integer.parseInt(result.get("pv").toString()) ? new BigDecimal(0)
                  : new BigDecimal(Integer.parseInt(result.get("pc").toString()) * 100)
                      .divide(new BigDecimal(result.get("pv").toString()), 3, RoundingMode.UP));
    }
    return result;
  }

  // public List<Map<String, Object>> findDataList(Map<String, Object> params) {
  // params.put("orderBy", "asc");
  // List<Map<String, Object>> list = rptFrontCampaignDailyMapper.findMemberRptData(params);
  // return list;
  // }

  public List<Map<String, Object>> findDataList(Map<String, Object> params,String startDate,String endDate) {
    params.put("orderBy", "asc");
    List<Map<String, Object>>  list = newRptFrontCampaignDailyMapper.findMemberRptData(params);
    List<String> times = TimeProcessingUtils.getDays(startDate,endDate);
    List<Map<String, Object>> news = new ArrayList<>();
    for(int i=times.size()-1;i>=0;i--){
      boolean count = false;
      for(int j=0;j<list.size();j++){
        String day = list.get(j).get("rdate")+"";
        if(times.get(i).equals(day)){
          count=true;
          news.add(list.get(j));
        }
      }
      if(!count){
        Map<String, Object> map = new HashMap<>();
        map.put("total_cost",new BigDecimal("0"));
        map.put("pv",0);
        map.put("pc",0);
        map.put("rdate",times.get(i));
        news.add(map);
      }
    }
    return news;
  }

  public List<Map<String, Object>> findRptHourList(Map<String, Object> params) {
    params.put("orderBy", "asc");
    List<Map<String, Object>> list = newRptFrontCampaignDailyMapper.findMemberRptHour(params);
    List<Map<String, Object>> news = new ArrayList<>();
    for(int i=0;i<24;i++){
      boolean count = false;
      for(int j=0;j<list.size();j++){
        Integer hour = Integer.parseInt(list.get(j).get("rdate")+"");
        if(i==hour){
          count=true;
          if(hour<10){
            list.get(j).put("date","0"+hour+":00");
          }else{
            list.get(j).put("date",hour+":00");
          }
          list.get(j).put("rdate",hour);
          news.add(list.get(j));
        }
      }
      if(!count){
        Map<String, Object> map = new HashMap<>();
        map.put("total_cost",new BigDecimal("0"));
        map.put("pv",0);
        map.put("pc",0);
        if(i<10){
          map.put("date","0"+i+":00");
        }else{
          map.put("date",i+":00");
        }
        map.put("rdate",i);
        news.add(map);
      }
    }
        return news;
  }

  public Map<String, Object> findTotalDataByAdGroup(Map<String, Object> params) {
    Map<String, Object> result = rptFrontAdGroupDailyMapper.findRptData(params);
    if (result != null) {
      result.put("ctr",
          result == null || result.get("pv") == null
              || 0 == Integer.parseInt(result.get("pv").toString()) ? new BigDecimal(0)
                  : new BigDecimal(Integer.parseInt(result.get("pc").toString()) * 100)
                      .divide(new BigDecimal(result.get("pv").toString()), 3, RoundingMode.UP));
    }
    return result;
  }

  public List<Map<String, Object>> findDataListByAdGorup(Map<String, Object> params) {
    params.put("orderBy", "asc");
    List<Map<String, Object>> list = rptFrontAdGroupDailyMapper.findMemberRptData(params);
    return list;
  }



}
